﻿<?php /*
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Sànnhàở</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="Stylesheet" href="css/style.css" type="text/css" />
    <script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" charset="utf-8">
		$(function(){
			$("select#provine").change(function(){
				$.getJSON("select.php",{id: $(this).val()}, function(j){
					var options = '';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
					}
					$("#district").html(options);
					$('#district option:first').attr('selected', 'selected');
				})				
			});
		})
	</script>
</head>
<body>
<?php require_once("connection/connect.php");?>
    <div class="wrapper">
    <?php require_once("top.php");?>
        <div class="body">
            <div class="cat-left">
                <div class="title-3">
                    Công cụ tìm kiếm
                </div>
                <form action="results.php" method="post">
                <div style="padding: 0">
                    <table cellpadding="2px">
                        <tr>
                            <td>
                            <select id="need" name="need" class="select">
                                <option value="" selected="selected">Chọn loại</option>
                                <option value='1'>Bán</option>
                                <option value='2'>Cho thuê</option>
                                <option value='3'>Cần thuê</option>
                                <option value='4'>Sang nhượng</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            <select id="purpose" name="purpose" class="select">
                                <option value="" selected="selected">Chọn mục đích</option>
                                <option value='1'>Nhà ở</option>
                                <option value='2'>Kinh doanh</option>
                                <option value='3'>Buôn bán</option>
                                <option value='4'>Đất trống</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select name="category" class="select">
                                <option value="">Chọn danh mục</option>
                                <?php
								$rs = mysql_query("select cat_id, name from categories");
								while($d = mysql_fetch_array($rs)){
								?>
                                <option value='<?php echo $d['cat_id'];?>'><?php echo $d['name'];?></option>
                                <?php }?>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="provine">
        						<option value="">Chọn Tỉnh/Thành Phố</option>
        <?php
		$rs = mysql_query("select provine_id, name from provines");
		while($d = mysql_fetch_array($rs)){
		?>
								<option value="<?php echo $d['provine_id'];?>"><?php echo $d['name'];?></option>
        <?php } ?>
							</select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                               <select id="district" name="district" class="select">
                                <option value="" selected="selected">Chọn quận/huyện</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <h4>
                                    Giá từ</h4>
                                    <input id="priceMin" name="priceMin" type="text"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                               <h4>
                                    đến</h4>
                                    <input id="priceMax" name="priceMax" type="text"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                               <h4>
                                    Diện tích</h4>
                                    <input id="area" name="area" type="text"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                 <select id="orientation" name="orientation" class="select">
                                                <option>Chọn hướng nhà</option>
                                            
                                                    <option value="1">Đông</option>
                                                    <option value="1">Tây</option>
                                                    <option value="1">Nam</option>
                                                    <option value="1">Bắc</option>
                                            
                              </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="livingroom" name="livingroom" class="select">
                                    <option value="" selected="selected">-- Phòng khách --</option>
                                    <option value='1'>1+</option>
                                    <option value='2'>2+</option>
                                    <option value='3'>3+</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="bedroom" name="bedroom" class="select">
                                    <option value="" selected="selected">-- Phòng ngủ --</option>
                                    <option value='1'>1+</option>
                                    <option value='2'>2+</option>
                                    <option value='3'>3+</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="bathroom" name="bathroom" class="select">
                                    <option value="" selected="selected">-- Phòng tắm --</option>
                                    <option value='1'>1+</option>
                                    <option value='2'>2+</option>
                                    <option value='3'>3+</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input id="full_comfortable" name="full_comfortable" type="checkbox" />Đầy đủ tiện nghi
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input id="btnSearch" name="btnSearch" class="button" type="submit" value="Tìm trong Sànnhàở.com" />
                            </td>
                        </tr>
                    </table>
                </div>
                </form>
            </div>
            <div class="cat-right">
                <table class="list-header">
                    <tr>
                        <td style="width: 160px">
                            Hình
                        </td>
                        <td style="width: 145px">
                            Chi tiết
                        </td>
                        <td style="width: 145px">
                            Người đăng
                        </td>
                        <td style="width: 145px">
                            Địa điểm
                        </td>
                        <td>
                            Ngày
                        </td>
                    </tr>
                </table>
<?php
if(isset($_POST['btnSearch'])){

if(isset($_POST['need'])){
	$need =	$_POST['need'];
}

if(isset($_POST['purpose'])){
	$purpose =	$_POST['purpose'];
}

if(isset($_POST['category'])){
	$category =	$_POST['category'];
}

if(isset($_POST['provine'])){
	$provine =	$_POST['provine'];
}

if(isset($_POST['district'])){
	$district =	$_POST['district'];
}

if(isset($_POST['priceMin'])){
	$priceMin =	$_POST['priceMin'];
}

if(isset($_POST['priceMax'])){
	$priceMax =	$_POST['priceMax'];
}

if(isset($_POST['area'])){
	$area =	$_POST['area'];
}

if(isset($_POST['orientation'])){
	$orientation =	$_POST['orientation'];
}

if(isset($_POST['livingroom'])){
	$livingroom =	$_POST['livingroom'];
}

if(isset($_POST['bedroom'])){
	$bedroom =	$_POST['bedroom'];
}

if(isset($_POST['bathroom'])){
	$bathroom =	$_POST['bathroom'];
}


if(isset($_POST['full_comfortable']) == 'on'){
	$full_comfortable =	'1';
}


}			
				$sql = sprintf("
				SELECT *
				FROM users, products, pro_images, prices, provines, districts, categories
				WHERE (products.product_id = pro_images.product_id) 
     				AND (products.product_id = prices.product_id)
     				AND (users.user_id = products.user_id)
     				AND (products.provine_id = provines.provine_id)
     				AND (products.district_id = districts.district_id)
     				AND (products.cat_id = categories.cat_id)
     				OR (products.need = %s)
     				OR (products.purpose = %s)
     				OR (products.cat_id = = %s)
					OR (products.provine_id = %s)
					OR (products.district_id = %s)
					OR (prices.price BETWEENT %s AND %s)
					OR (products.area >= %s)
					OR (products.orientation >= %s)
					OR (products.livingroom >= %s)
					OR (products.bedroom >= %s)
					OR (products.bathroom >= %s)
					OR (products.full_comfortable = '%s')",
					$need,
					$purpose,
					$category,
					$provine,
					$district,
					$priceMin,
					$priceMax,
					$area,
					$orientation,
					$livingroom,
					$bedroom,
					$bathroom,
					$full_comfortable);
						
					$rs = mysql_query($sql);						
					if($nrow = mysql_num_rows($rs) < 1){
						echo "Không có sản phẩm";
					}else{
				while($d = mysql_fetch_array($rs)){
				?>
                <table cellpadding="5px" style="line-height: 20px; border-bottom: 1px solid #ccc;
                    padding-bottom: 10px; width: 100%; margin-top: 10px">
                    <tr>
                        <td style="width: 150px; border-right: 1px solid #ff9900; vertical-align: top">
                            <img alt="" border="0" src="images/<?php echo $d["n2"];?>" width="140px" />
                        </td>
                        <td style="width: 135px; border-right: 1px solid #ff9900; vertical-align: top">
                            <span style="color: #dd4400; font-weight: bold"><?php echo $d["n1"];?><br />
                            </span><span class="text-1">$:</span> <?php echo number_format($d["price"],0);?> VND<br />
                            <span class="text-1">Diện tích:</span> <?php echo number_format($d["area"],0);?> m2<br />
                            <span class="text-1">Phòng khách:</span> <?php echo $d["livingroom"];?><br />
                            <span class="text-1">Phòng ngủ:</span> <?php echo $d["bedroom"];?><br />
                            <span class="text-1">Phòng tắm:</span> <?php echo $d["bathroom"];?>
                        </td>
                        <td style="width: 135px; border-right: 1px solid #ff9900; vertical-align: top">
                            <?php echo $d["username"];?><br />
                        </td>
                        <td style="width: 135px; border-right: 1px solid #ff9900; vertical-align: top">
                            <?php echo $d["address"];?> 
                        </td>
                        <td style="vertical-align: top">
                            <?php echo date(('d/m/Y'), strtotime($d["datecreate"]));?>
                        </td>
                    </tr>
                </table>
                <?php }
					}?>
                </div>
            <div class="vide">
            </div>
        </div>
    </div>
    <?php require_once("footer.php");?>
</body>
</html>
